package com.xian.gaoxiao.db;

import com.xian.gaoxiao.db.CommentTable.CommentColumns;
import com.xian.gaoxiao.db.TaskItemTable.TaskItemColumns;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AppDBManager implements AppDBConstants {

	private static final String TAG = "AppDBManager";

	private SQLiteDatabase mDb;

	public AppDBManager(SQLiteDatabase db) {
		mDb = db;

	}

	public void onCreateV1() {
		create_tables_v1();
		create_views_v1();
		create_triggers_v1();
		init_data_v1();
	}

	public void onCreateV2() {

	}

	public void onUpgradeV1toV2() {
	}

	public void onUpgradeV2toV3() {
	}

	private void create_tables_v1() {
		create_t_task_list();
		create_t_task_comment();
	}

	private void init_data_v1() {
		init_t_task_list();
		init_t_task_comment();

	}

	private void create_triggers_v1() {

	}

	private void create_views_v1() {
	}

	public static String getColumnData(SQLiteDatabase db, String tableName,
			String whereStr, String[] whereArgs, String columnName) {
		Cursor cursor = null;
		String result = null;
		try {
			StringBuilder selectBuilder = new StringBuilder();
			selectBuilder.append(" SELECT ");
			selectBuilder.append(columnName);
			selectBuilder.append(" FROM ");
			selectBuilder.append(tableName);
			selectBuilder.append(" WHERE ");
			selectBuilder.append(whereStr);
			cursor = db.rawQuery(selectBuilder.toString(), whereArgs);
			if (cursor.moveToLast()) {
				result = cursor.getString(0);
			}
			return result;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}

	}



	private void create_t_task_list() {

		StringBuilder builder = new StringBuilder();
		builder.append("CREATE TABLE IF NOT EXISTS ");
		builder.append(Tables.TASKITEM);
		builder.append(" (");
		builder.append(TaskItemColumns.ID);
		builder.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
		builder.append(TaskItemColumns.CREATEDATE);
		builder.append(" LONG   ,");
		builder.append(TaskItemColumns.ALERTDATE);
		builder.append(" LONG   ,");
		builder.append(TaskItemColumns.ENABLEALERT);
		builder.append(" INTEGER DEFAULT 0 ,");
		builder.append(TaskItemColumns.BEGINDATE);
		builder.append(" LONG   ,");
		builder.append(TaskItemColumns.FINISHDATE);
		builder.append(" LONG   ,");
		builder.append(TaskItemColumns.MODIFYDATE);
		builder.append(" LONG   ,");
		builder.append(TaskItemColumns.STATUS);
		builder.append(" INTEGER DEFAULT 0 ,");
		builder.append(TaskItemColumns.TITLE);
		builder.append(" TEXT ,");
		builder.append(TaskItemColumns.RANK);
		builder.append(" INTEGER ,");
		builder.append(TaskItemColumns.SORT);
		builder.append(" INTEGER DEFAULT 0 ,");
		builder.append(TaskItemColumns.DATA);
		builder.append(" TEXT  ,");
		builder.append(TaskItemColumns.PIC);
		builder.append(" BLOB ,");
		builder.append(TaskItemColumns.SOUND);
		builder.append(" TEXT );");

		Log.e(TAG, builder.toString());
		mDb.execSQL(builder.toString());

	}

	private void create_t_task_comment() {

		StringBuilder builder = new StringBuilder();
		builder.append("CREATE TABLE IF NOT EXISTS ");
		builder.append(Tables.TASKCOMMENT);
		builder.append(" ( ");
		builder.append(CommentColumns.ID);
		builder.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
		builder.append(CommentColumns.TASKID);
		builder.append(" INTEGER NOT NULL ,");
		builder.append(CommentColumns.CREATEDATE);
		builder.append(" LONG ,");
		builder.append(CommentColumns.MODIFYDATE);
		builder.append(" LONG ,");
		builder.append(CommentColumns.ISSHOW);
		builder.append(" INTEGER DEFAULT 0 ,");
		builder.append(CommentColumns.COMMENT);
		builder.append(" TEXT ,");
		builder.append(CommentColumns.SORT);
		builder.append(" INTEGER DEFAULT 0 ,");
		builder.append(CommentColumns.PIC);
		builder.append(" BLOB );");

		Log.e(TAG, builder.toString());
		mDb.execSQL(builder.toString());

	}

	private void init_t_task_list() {
		TaskItemTable mTaskList = new TaskItemTable(mDb);
		mTaskList.insert(null, 0, null, null,  "点击这里试试", 0, null, null, null);
		mTaskList.insert(null, 0, null, null,  "点击其他被缩小色块，也可以放大他们哦~", 0, null, null, null);
		mTaskList.insert(null, 0, null,  null,  "点击左侧多选框，完成这件事情吧", 0, null, null, null);
		mTaskList.insert(null, 0, null,  null,  "点击我，查看我的详情", 0, null, null, null);
		mTaskList.insert(null, 0, null,  null,  "点击右上部“+”添加一件事情", 1, null, null, null);
		
		mTaskList.insert(null, 0, null,  null,  "点击左上面按钮拉出更多功能选项", 1, null, null, null);
		mTaskList.insert(null, 0, null,  null,  "开始日期为明天的事情显示在“以后”里面", 2, null, null, null);
		mTaskList.insert(null, 0, null,  null, "在“记录”里面可以看到所有的事情", 2 ,null, null, null);
		mTaskList.insert(null, 0, null, null,  "还有更多功能，慢慢探索吧~", 3, null, null, null);
		mTaskList.insert(null, 0, null , null, "记得给我好评哦~(^_^)", 3, null, null, null);
	}

	private void init_t_task_comment() {

	}

}